import pymel.core as pm
import yaml

class AssignAssetMaterials(object):
    def __init__(asset_data):
        self.asset_data = asset_data
        
    def load_yaml_data(self, file_path):
        with open(file_path, 'r') as f:
            data = yaml.load(f)
        return data
        
    def assign_material(self):
        for asset in self.asset_data:
            namespace_list = []
            engine_list = []
            # 获取资产的所有namespace
            data = asset_data[asset]
            for ref in data["items"]:
                namespace_list.append(pm.PyNode(ref).referenceFile().namespace)
                
            # 获取shaderNetwork的namespace
            shader_network_ref = data["shader_network"]
            shader_namespace = pm.PyNode(shader_network_ref).referenceFile().namespace
            
            # 获取关系数据 
            shader_mapping_file = data["shader_mapping"]
            shader_mapping_data = self.load_yaml_data(shader_mapping_file)
            
            # 解析namespace并赋予材质
            for engine in shader_mapping_data:
                final_engine = ':'.join([shader_namespace,engine])
                print final_engine
                element_list = shader_mapping_data[engine]
                final_element_list = [element.replace("|", "|{}:".format(namespace)) for namespace in namespace_list for element in element_list]
                pm.sets(final_engine, forceElement=final_element_list)

